'''
Created on Mar 23, 2013

@author: Petko
'''
from django.views.generic.base import View
from sent.model.entities.user import Group
from sent.model.UserDBManager import UserDBManager
from sent.utils import createValidToken, CommonResult, ErrorIds
from sent.utils.permissions import parseScopeList
from sent.utils.result_printer import getPrinter
import re

groupNameRegex = re.compile(r"^[a-zA-Z0-9_\+\=\/\.\,\\\!\@\#\$\%\^\&\*\(\)]+$")

class CreateGroup(View):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        
    def post(self, *args, **kwargs):
        accessTokenStr = self.request.POST.get('access_token', None)
        group_id = self.request.POST.get('group_id', None)
        group_data = self.request.POST.get('group_data', None)
        permissions = self.request.POST.get('permissions', None)
        
        result = CommonResult()
        
        if accessTokenStr and group_id and permissions:
            if groupNameRegex.match(group_id):
                accessToken, error = createValidToken(accessTokenStr, 'group_adm')
                if accessToken:
                    permissionsSet = parseScopeList(permissions)
                    
                    scopesValid = True
                    for scope in permissionsSet:
                        if not scope.isGroupScope:
                            scopesValid = False
                            result.result = "Unknown scope %s" % str(scope)
                            
                    if scopesValid:
                        dbMan = UserDBManager(False)
                        try:
                            gr = dbMan.getGroupByExtId(group_id)
                            if gr:
                                gr.external_data = group_data
                            else:
                                gr = Group()
                                gr.external_id = group_id
                                if group_data:
                                    gr.external_data = group_data
                                dbMan.createGroup(gr)
                            dbMan.setGroupPermissions(gr.id, permissionsSet)
                            
                            result.errorId = ErrorIds.SUCCESS
                            result.result = gr.toPrimitiveDict()
                            
                            dbMan.commitSession()
                        except:
                            dbMan.rollbackSession()
                    else:
                        result.errorId = ErrorIds.BADREQUEST
                else:
                    result.errorId = ErrorIds.UNAUTHORIZED
                    result.result = error
            else:
                result.errorId = ErrorIds.BADREQUEST
                result.result = "Invalid character in group_id"
        else:
            result.errorId = ErrorIds.BADREQUEST
            result.result = "Missing parameter"
            
        return getPrinter().printResult(result) 